package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduQuestionMapper;
import com.zenith.edu.po.domain.EduQuestion;
import com.zenith.edu.po.dto.EduQuestionDTO;
import com.zenith.edu.po.vo.EduQuestionVO;
import com.zenith.edu.service.EduQuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduQuestionServiceImpl extends ServiceImpl<EduQuestionMapper, EduQuestion> implements EduQuestionService {
    
    @Override
    public EduQuestionVO createQuestion(EduQuestionDTO dto) {
        EduQuestion question = new EduQuestion();
        BeanUtils.copyProperties(dto, question);
        this.save(question);
        
        EduQuestionVO vo = new EduQuestionVO();
        BeanUtils.copyProperties(question, vo);
        return vo;
    }
    
    @Override
    public EduQuestionVO getQuestionById(Long id) {
        EduQuestion question = this.getById(id);
        if (question == null) {
            return null;
        }
        
        EduQuestionVO vo = new EduQuestionVO();
        BeanUtils.copyProperties(question, vo);
        return vo;
    }
    
    @Override
    public EduQuestionVO updateQuestion(EduQuestionDTO dto) {
        EduQuestion question = new EduQuestion();
        BeanUtils.copyProperties(dto, question);
        this.updateById(question);
        
        EduQuestionVO vo = new EduQuestionVO();
        BeanUtils.copyProperties(question, vo);
        return vo;
    }
    
    @Override
    public boolean deleteQuestion(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduQuestionVO> getQuestionList() {
        List<EduQuestion> questionList = this.list();
        return questionList.stream().map(question -> {
            EduQuestionVO vo = new EduQuestionVO();
            BeanUtils.copyProperties(question, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}